{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a1e60026",
   "metadata": {},
   "source": [
    "# 什么是集合\n",
    "数学中的集合是一个独特元素的集合。\n",
    "\n",
    "集合用于涉及频繁的相交、联合和差异操作。\n",
    "\n",
    "# 在NumPy中创建集合\n",
    "我们可以使用NumPy的unique()方法从任何数组中找到唯一元素。例如，创建一个集合数组，但要记住，集合数组只能是1-D数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "01a83167",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([1, 1, 1, 2, 3, 4, 5, 5, 6, 7])\n",
    "\n",
    "x = np.unique(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c0e2a2a",
   "metadata": {},
   "source": [
    "# 寻找联合\n",
    "要找到两个数组的唯一值，请使用union1d()方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a3670802",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3, 4])\n",
    "arr2 = np.array([3, 4, 5, 6])\n",
    "\n",
    "newarr = np.union1d(arr1, arr2)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5436a2b",
   "metadata": {},
   "source": [
    "# 寻找相同值\n",
    "要想只找到两个数组中都有的值，请使用intersect1d()方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d0701495",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4]\n"
     ]
    }
   ],
   "source": [
    "# intersect1d()方法需要一个可选的参数assume_unique，\n",
    "# 如果设置为True，可以加快计算速度。在处理集合的时候，它应该总是被设置为 \"真\"。\n",
    "newarr = np.intersect1d(arr1, arr2, assume_unique=True)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "febb562a",
   "metadata": {},
   "source": [
    "# 寻找差异\n",
    "要想只找到第一组中不存在于第二组中的数值，请使用setdiff1d()方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f7260434",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2]\n"
     ]
    }
   ],
   "source": [
    "newarr = np.setdiff1d(set1, set2, assume_unique=True)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34f35b54",
   "metadata": {},
   "source": [
    "# 寻找异或值\n",
    "要想只找到两个集合的不相等值，请使用setxor1d()方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cf6b21a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 5 6]\n"
     ]
    }
   ],
   "source": [
    "newarr = np.setxor1d(set1, set2, assume_unique=True)\n",
    "\n",
    "print(newarr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
